<?php
/**
 * 物流配送方式
 * @author tangbl
 * 
 */
class ShippingAction extends CommonAction {
	
	//初始化
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
		
	/**
	 * 配送方式列表
	 */
	public function index() {
		if(IS_AJAX){
			
			import('ORG.Util.Page');
			$model     = D("Shipping");
			
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","sort");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			//$map       = "isdel=1 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->field("print_ship,config_lable",true)
						->order($orderName.' '.$orderDesc)->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$ship = "设置配送区域";
			$edit = "修改配送方式";
			$disa = "禁用配送方式";
			$actv = "激活配送方式";
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				if($row['isdel']==1){
					$isactive = $active;
					$html = 'class="btn icn-only red   delAlone" title="'.$disa.'" filed="2"';
				}else{
					$isactive = $frozen;
					$html = 'class="btn icn-only green delAlone" title="'.$actv.'" filed="1"';
				}
				if($row['ispay']==1){
					$ispay = "是";
				}else{
					$ispay = "否";
				}
				$entry = array('id'=>$row['id'],
					'cell'=>array(
							'id'       => $row['id'],
							'title'    => $row['title'],
							'remark'   => $row['remark'],
							'insure'   => $row['insure'].'%',
							'ispay'    => $ispay,
							'deal'=>'
						 <a href="'.__URL__.'/area_index/id/'.$row['id'].'" title="'.$ship.'" class="btn green"><i class="icon-truck"></i></a>
						 <a href="'.__URL__.'/edit/id/'.$row['id'].'" title="'.$edit.'" class="btn blue"><i class="icon-edit"></i></a>
						 <a href="#" '.$html.' data-id="'.$row['id'].'"><i class="icon-key"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
			
		}else{
			$this->display();
		}
	}
	
	/**
	 * 配送方式对应配送区域列表
	 */
	public function area_index() {
		
		if(IS_AJAX){
				
			import('ORG.Util.Page');
			$model     = D("ShippingArea");
				
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			//$shipid    = $this->_get("shipid","intval",1);
				
			$map       = unescape($this->_get("where"));
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->field("configure",true)
						 ->order($orderName.' '.$orderDesc)->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
				
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'title'    => $row['title'],
						'regions'  => $model->showRegions($row['id']),
						'deal'=>'
					 <a href="'.__URL__.'/area_edit/id/'.$row['id'].'" class="btn blue"><i class="icon-edit"></i></a>
					 <a href="#" class="btn red delAlone"  data-id="'.$row['id'].'"><i class="icon-remove"></i></a>'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
				
		}else{
			$id   = $this->_param("id","intval",0);
			$rows = M("Shipping")->field("title,id")->find($id);
			$this->assign("detail",$rows);
			$this->display();
		}
	}
	
	/**
	 * 添加配送区域
	 */
	public function area_add(){

		if(IS_POST){
			$model   = D("ShippingArea");
			$shipid  = $this->_post("shipid","intval",0);
			if($model->create()){

				$regions = $this->_post("regions");
				$ships   = $this->_post("ship");
				$model->configure = json_encode($ships);
					
				$id  = $model->add();
				//更新配送区域对应的地址关联表
				$model->createShipArea($id,$shipid,$regions);
			}
			$this->success(L('cfg_form_tipmsg_addsucc'),U("Shipping/area_index",'id='.$shipid));
		}else{
			
			$shipid = $this->_param("shipid","intval",0);
			if(!$shipid) $this->error(L('cfg_form_tipmsg_illgel'));
			
			$lang    = getLang();
			$options = getDictByKey("Dict_address",$lang);
			$rows['json']   = $options["array"];
			$rows['shipid'] = $shipid;
			$rows['parent'] = M("Shipping")->where("id=".$shipid)->getField("title");
			
			$this->assign("detail",$rows);
			$this->display();
		}
	}
	
	/**
	 * 修改配送区域
	 */
	public function area_edit(){
		
		$model   = D("ShippingArea");
		if(IS_POST){
			$shipid = $this->_post("shipid","intval",0);
			if($model->create()){
				$id      = $this->_post("id","intval",0);
				$regions = $this->_post("regions");
				$ships   = $this->_post("ship");
				$model->configure = json_encode($ships);
				
				$flag    = $model->save();
				//更新配送区域对应的地址关联表
				$model->createShipArea($id,$shipid,$regions);
			}
			$this->success(L('cfg_form_tipmsg_editsucc'),U("Shipping/area_index",'id='.$shipid));
		}else{

			$id   = $this->_param("id","intval",0);
			$rows = $model->find($id);
			if(!$rows['id']) $this->error(L('cfg_form_tipmsg_illgel'));
			
			$ship = json_decode($rows['configure'],true);
			$this->assign("ship",$ship);
			
			$lang    = getLang();
			$options = getDictByKey("Dict_address",$lang);
			$rows['json']    = $options["array"];
			
			$rows['parent']  = M("Shipping")->where("id=".$rows['shipid'])->getField("title");
			$rows['regions'] = $model->getShipRegions($id);
			
			$this->assign("detail",$rows);
			$this->display();
		}
	}
	
	/**
	 * 添加配送区域
	 */
	public function area_del(){
	
	}
	
	/**
	 * 添加
	 */
	public function add(){
		
		if(IS_POST){
			$param = array(
				"reply"  => false,
				"cache"  => false
			);
			parent::do_add("Shipping",$param);
		} else {
		
			$this->display();
		}
	}
	
	/**
	 * 禁用或激活
	 */
	public function del(){
		
		$id  = $this->_get("id","intval",0);
		$act = $this->_get("act","intval",2);
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		$model = D("Shipping");
		$map['id']    = $id;
		$map['isdel'] = $act;
		
		$res = $model->save($map);
		if($res){
			$this->success(L("cfg_form_tipmsg_editsucc"));
		}else{
			$this->error(L("cfg_form_tipmsg_editfail"));
		}
	}
	
	/**
	 * 修改
	 */
	public function edit(){
		
		if(IS_POST) {
			$param = array(
				"reply"  => false,
				"cache"  => false
			);
			parent::do_edit("Shipping",$param);
		}else{
			$atlas  = D("Shipping");
			$id     = $this->_param('id','intval',0);
			$data   = $atlas->find($id);
			if(!$data['id']) $this->error(L('cfg_form_tipmsg_illgel'));

			$this->assign("detail",$data);
		
			$this->display();
		}
	}
	
}
?>